package com.qqtech.qquan.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

import com.qqtech.core.frame.controller.BaseControllerImpl;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.sys.enums.SysConst;
import com.qqtech.qquan.user.model.UserAdmin;

@SuppressWarnings("rawtypes")
@Controller
public class BaseAgentController extends BaseControllerImpl {

	@Override
	protected BaseService getBaseService() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 获取当前管理员对应的社区id
	 * 
	 * @return
	 */
	protected Integer getCurrCommunityId() {
		UserAdmin userAdmin = this.getLoginUser();
		if (userAdmin != null && userAdmin.getCommunityId() != null && userAdmin.getCommunityId().intValue() != 0) {
			return userAdmin.getCommunityId();
		}
		return -1;
	}

	protected UserAdmin getLoginUser(HttpServletRequest request, HttpServletResponse response) {
		return (UserAdmin) request.getSession().getAttribute(SysConst.AGENT_USER_SESSION);
	}

	/**
	 * 重载获取登录用户的信息
	 * 
	 * @return
	 */
	protected UserAdmin getLoginUser() {
		return (UserAdmin) getRequest().getSession().getAttribute(SysConst.AGENT_USER_SESSION);
	}

	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return request;
	}

	public HttpServletResponse getResponse() {
		HttpServletResponse response = ((ServletWebRequest) RequestContextHolder.getRequestAttributes()).getResponse();
		return response;
	}

	/**
	 * 获取HttpSession对象
	 * 
	 * @return
	 */
	public HttpSession getSession() {
		return getRequest().getSession(true);
	}

	/**
	 * 获取请求参数
	 */
	protected String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	/**
	 * 获取请求参数数组
	 */
	protected String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}

	/**
	 * 获取int类型请求参数
	 */
	protected int getIntParameter(String name) {
		return Integer.parseInt(getParameter(name));
	}

	/**
	 * 获取int类型请求参数,当出错时,返回defaultValue
	 */
	protected int getIntParameter(String name, int defaultValue) {
		try {
			return Integer.parseInt(getParameter(name));
		} catch (Exception e) {
			return defaultValue;
		}
	}

	/**
	 * 设置request attr
	 * 
	 * @param name
	 * @param value
	 */
	public void setAttribute(String name, Object value) {
		getRequest().setAttribute(name, value);
	}

	/**
	 * 读取request attr
	 * 
	 * @param name
	 * @return
	 */
	public Object getAttribute(String name) {
		return getRequest().getAttribute(name);
	}

}
